package com.leyou.order.config;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.leyou.common.constants.LyConstants;
import com.leyou.common.constants.MQConstants;

import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ 配置类
 * <p>
 * 说明：
 * 1. 定义超时支付订单队列
 * 2. 定义广播模式的延时交换机
 * 3. 绑定超时支付订单队列与延时交换机
 * 4. 定义消息模板用于发布消息，并且设置其消息转换器
 *
 * @author MoCha
 * @date 2020/4/14
 */
@Configuration
public class RabbitPluginConfig  {
    /**
     * 延时队列 还库
     * @return
     */
    @Bean
    public Queue delayQueue(){
        return new Queue(MQConstants.Queue.REPAY_STOCK_QUEUE,true);
    }

    //延时队列交换机  还库
    @Bean
    public CustomExchange delayExchange(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MQConstants.Exchange.REPAY_STOCK_EXCHANGE,"x-delayed-message",true, false,args);
    }

    /**
     * 给延时队列绑定交换机  还库
     * @return
     */
    @Bean
    public Binding cfgDelayBinding(Queue delayQueue, CustomExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(MQConstants.RoutingKey.REPAY_STOCK_KEY).noargs();
    }

    /**
     * 延时队列  更改订单状态
     * @return
     */
    @Bean
    public Queue changeQueue(){
        return new Queue(MQConstants.Queue.CHANGE_ORDER_STATE_QUEUE,true);
    }

    //延时队列交换机  更改订单状态
    @Bean
    public CustomExchange changeExchange(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MQConstants.Exchange.CHANGE_ORDER_STATE_EXCHANGE,"x-delayed-message",true, false,args);
    }

    /**
     * 给延时队列绑定交换机  更改订单状态
     * @return
     */
    @Bean
    public Binding changeBinding(Queue changeQueue, CustomExchange changeExchange){
        return BindingBuilder.bind(changeQueue).to(changeExchange).with(MQConstants.RoutingKey.CHANGE_ORDER_STATE_KRY).noargs();
    }

}






